/*
 * The MIT License
 *
 * Copyright 2011 http://generic-toolset.googlecode.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package skat.util;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ForgetfulList<T> extends AbstractList<T> {

    private List<Entry> entries = new ArrayList<Entry>();
    private double defaultForgettingRate = .2;

    public ForgetfulList() {
    }

    public ForgetfulList(double defaultRate) {
        setDefaultForgettingRate(defaultRate);
    }

    private void forget() {
        for (int i = 0; i < entries.size(); i++) {
            if (entries.get(i).isLose()) {
                entries.remove(entries.get(i));
            }
        }
    }

    public double getDefaultForgettingRate() {
        return defaultForgettingRate;
    }

    public void setDefaultForgettingRate(double defaultForgettingRate) {
        this.defaultForgettingRate = defaultForgettingRate;
    }
    
    @Override
    public int size() {
        return entries.size();
    }

    @Override
    public boolean add(T entry) {
        return add(entry, getDefaultForgettingRate());
    }

    public boolean add(T entry, double forgetrate) {
        boolean result = entries.add(new Entry(forgetrate, entry));
        if (result) {
            forget();
        }

        return result;

    }

    @Override
    public T get(int index) {
        return (T) entries.get(index).getEntry();
    }

    @Override
    public String toString() {

        String result = "";
        for (T e : this) {
            result += e.toString() + "\n";
        }

        return result;
    }

    private class Entry  {

        private double rate, sum = 0;
        private T entry;

        public Entry(double rate, T entry) {
            this.rate = rate;
            this.entry = entry;
        }

        public T getEntry() {
            return entry;
        }

        public double getRate() {
            return rate;
        }

        public double getSum() {
            return sum;
        }

        public boolean isLose() {
            sum += rate;
            return (sum > 1);
        }

     
        @Override
        public String toString() {
            return entry.toString() + " (life remaining: " + (int) ((1 - getSum()) / getRate()) + ")";
        }
    }
}
